package Fraction;

public class Fraction {
    int fenzi;
    int fenmu;

    Fraction(int fenzi, int fenmu) {
        this.fenzi = fenzi;
        this.fenmu = fenmu;
    }

    public Fraction() {
    }

    public double toDouble() {
        return (double) fenzi / fenmu;
    }

    public Fraction plus(Fraction r) {
        Fraction newFraction = new Fraction();
        newFraction.fenmu = this.fenmu * r.fenmu;
        newFraction.fenzi = this.fenzi * r.fenmu + this.fenmu * r.fenzi;
        return newFraction;
    }

    public Fraction multiply(Fraction r) {
        Fraction newFraction = new Fraction();
        newFraction.fenmu = this.fenmu * r.fenmu;
        newFraction.fenzi = this.fenzi * r.fenzi;
        return newFraction;
    }

    public void print() {
        int r, x = fenzi, y = fenmu;
        // 化简
        while (x != 0) {
            r = y % x;
            y = x;
            x = r;
        }
        fenzi /= y;
        fenmu /= y;
        if (fenzi == fenmu) {
            System.out.println(1);
        } else {
            System.out.println(fenzi + "/" + fenmu);
        }
    }
}
